package com.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wms.entity.WProductCategory;
import com.wms.dao.WProductCategoryDao;
import com.wms.mytool.Result;
import com.wms.service.WProductCategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 商品类别表(WProductCategory)表服务实现类
 *
 * @author makejava
 * @since 2023-10-20 13:21:28
 */
@Service("wProductCategoryService")
public class WProductCategoryServiceImpl implements WProductCategoryService {
    @Resource
    private WProductCategoryDao wProductCategoryDao;

    /**
     * 查询所有商品类别
     * @return 实例对象
     */
    @Override
    public Result selectAll() {
        List<WProductCategory> wProductCategories = this.wProductCategoryDao.selectByMap(null);
        if(wProductCategories!=null){
            return new Result(200,wProductCategories,"查询成功");
        }else {
            return new Result(400,null,"查询失败");
        }
    }

    /**
     * 添加商品类别
     * @param wProductCategory 商品类别信息
     * @return 实例对象
     */
    @Override
    public Result addProductCategory(WProductCategory wProductCategory) {
        wProductCategory.setCreateTime(new Date());
        wProductCategory.setUpdateTime(new Date());
        int i = this.wProductCategoryDao.insert(wProductCategory);
        if(i==1){
            return new Result(200,i,"添加成功");
        }else{
            return new Result(400,null,"添加失败");
        }
    }

    /**
     * 修改商品类别
     * @param wProductCategory 商品类别信息
     * @return 实例对象
     */
    @Override
    public Result updateById(WProductCategory wProductCategory) {
        wProductCategory.setUpdateTime(new Date());
        int i = this.wProductCategoryDao.updateById(wProductCategory);
        if(i==1){
            return new Result(200,i,"修改成功");
        }else{
            return new Result(400,null,"修改失败");
        }
    }

    /**
     * 删除商品类别
     * @param id 商品类别id
     * @return 实例对象
     */
    @Override
    public Result deleteProductCategory(Integer id) {
        try {
            QueryWrapper<WProductCategory> qw = Wrappers.query();
            qw.eq("id", id);
            int i = this.wProductCategoryDao.delete(qw);
            if(i==1){
                return new Result(200,i,"删除成功");
            }else{
                return new Result(400,null,"删除失败");
            }
        }catch (Exception e){
            return new Result(400,null,"存在关联商品，不可删除");
        }

    }
}
